﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WeiboPlus.Service;
using System.Collections.Generic;
using WeiboPlus.Models;
using System.Linq;
using System.Threading;

namespace WeiboPlus
{
    public class CompositeUserService : IUserService
    {
        IDictionary<SNSAccount, IUserService> _svcs = new Dictionary<SNSAccount, IUserService>();

        public bool IsActive
        {
            get
            {
                return _svcs.Keys.Any(key => key.IsActive);
            }
        }

        public void Register(SNSAccount account, IUserService svc)
        {
            if (!_svcs.ContainsKey(account))
            {
                _svcs.Add(account, svc);
            }
        }

        public void Remove(SNSAccount account)
        {
            if (_svcs.ContainsKey(account))
            {
                _svcs.Remove(account);
            }
        }

        public bool Contains(SNSAccount account)
        {
            return _svcs.ContainsKey(account);
        }

        public void GetUser(Models.SNSRequest request, Action<Models.SNSUser, Models.SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void GetUserList(Models.SNSRequest snsRequest, Action<System.Collections.Generic.IList<Models.SNSUser>, Models.SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void Follow(Models.SNSUser user, Action<Models.SNSUser, Models.SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void Unfollow(Models.SNSUser user, Action<Models.SNSUser, Models.SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void GetFriendship(Models.SNSUser user, Action<Models.SNSFriendship, Models.SNSResult> action)
        {
            throw new NotImplementedException();
        }

        public void SearchUser(Models.SNSRequest snsRequest, Action<System.Collections.Generic.IList<Models.SNSUser>, Models.SNSResult> action)
        {
            if (!_svcs.Keys.Any(key => key.IsActive))
            {
                if (action != null)
                {
                    action(null, new SNSResult() { IsSuccess = false, Message = AppResx.NoBindedAccount });
                }
                return;
            }
            ThreadPool.QueueUserWorkItem((o) =>
            {
                foreach (var svc in _svcs)
                {
                    if (svc.Key.IsActive && !svc.Key.IsDuplicate)
                        svc.Value.SearchUser(snsRequest, action);
                    Thread.Sleep(100);
                }
            });
        }

        public Models.SNSAccount Account
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public SNSProvider Provider
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Func<SNSProvider> ProviderResolver
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public IMappingFactory Factory
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Func<IMappingFactory> FactoryResolver
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
